/**
 * Created by WT-SUN on 2017/4/12.
 */
$(function () {
    queryDepartmentTree();
    $("#factorName").textbox({
        iconWidth: 20,
        icons: [{
            iconCls: 'icon-add',
            handler: function(e){
                add_hazardFactor_func();
            }
        },{
            iconCls: 'icon-remove',
            handler: function(e){
                $(e.data.target).textbox('clear');//
                $('#factorId').val('');
            }
        }]
    });
    queryfactorIds();
    //factorId
});
function queryfactorIds(){
    var pointId=$("#monid").val();
    $.ajax({
        url:ROOT_PATH + '/occupationalHealth/monPoint/selectHarmIds.shtml',//提交地址
        data:{id : pointId},//将表单数据序列化
        type:"POST",
        dataType:"json",
        success:function(result){
            if (result.length>1){
                var names = '';
                var ids='';
                for (var index in result) {
                    names += ',' + result[index].name;
                    ids += ',' + result[index].factorId;
                }
                $("#factorName").textbox('setValue', names.substring(1)).textbox('setText', names.substring(1));
                $("#factorId").val(ids.substring(1));
            }else {
                $("#factorName").textbox('setValue', result[0].name).textbox('setText', result[0].name);
                $("#factorId").val(result[0].factorId);
            }
        }
    });
}

var add_hazardFactor_func=function () {
    WindowManager.openLayer({
        type: 2,
        title: '选择危害因素',
        area: ['900px', '60%'],
        content: ROOT_PATH + '/occupationalHealth/hazardFactor/listTb.shtml?isMulti=0',
        callBack:function(data){
            getSelectedFactor(data);
        }
    });
}
var getSelectedFactor = function (data) {
    if (data.length>1){
        var names = '';
        var ids='';
        for (var index in data) {
            names += ',' + data[index].name;
            ids += ',' + data[index].id;
        }
        $("#factorName").textbox('setValue', names.substring(1)).textbox('setText', names.substring(1));
        $("#factorId").val(ids.substring(1));
    }else {
        $("#factorName").textbox('setValue', data[0].name).textbox('setText', data[0].name);
        $("#factorId").val(data[0].id);
    }
};
/**
 * 查询部门树方法
 */
function queryDepartmentTree(){
    $.ajax({
        url:ROOT_PATH + '/permission/department/getDepartmentTree.shtml',//提交地址
        data:{parentId : 0},//将表单数据序列化
        type:"POST",
        dataType:"json",
        success:function(result){
            $('#companyName').combotree(
                {
                    data:result,
                    require:true,
                    prompt:'请选择',
                    loadFilter:function(data){
                        var newData = new Array();
                        newData.push({id:"",text:'请选择'});
                        newData.push(data[0]);
                        return newData;
                    }
                }
            );
        }
    });
}
/**
 * 提交表单
 */
function submitForm() {
    var factorName=$('#factorName').val();
    if (factorName==''){
        layer.alert('请正确填写监测项目名称', {icon: 5});
        return;
    }
    var factorId=$('#factorId').val();
    if (factorId==''){
        layer.alert('监测项目不存在', {icon: 5});
        return;
    }
    var companyName=$('#companyName').val();
    if (companyName==''){
        layer.alert('请选择单位', {icon: 5});
        return;
    }
    $('#pointEditForm').form('submit', {
        url: ROOT_PATH + '/occupationalHealth/monPoint/updateByIdSelective.shtml',
        onSubmit: function (param) {
            if (!$(this).form('validate')) {
                layer.alert('请正确填写信息', {icon: 5});
            }
            return $(this).form('validate');
        },
        success: function (result) {
            result = $.parseJSON(result);
            if (result.success) {
                layer.msg('保存成功', {
                    icon: 1,
                    time: 1000 //2秒关闭（如果不配置，默认是3秒）
                }, function () {
                    var indexp = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(indexp); //再执行关闭
                    window.parent.reLoadData();
                });
            }
            else {
                layer.msg('保存失败', {
                    icon: 2,
                    time: 1000
                });
            }
        }
    });
}